﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>Model Namespace Tests</title>
		
		<link href="../Common/Styles/qunit.css" type="text/css" rel="stylesheet" />

		<script src="../Common/Scripts/JQuery/jquery-1.3.2.js" type="text/javascript"></script>
		<script src="../Common/Scripts/QUnit/qunit.js" type="text/javascript"></script>
		<script src="../Common/Scripts/Microsoft/MicrosoftAjax.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.model.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.mapper.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.mock.js" type="text/javascript"></script>
		<script src="../Common/Scripts/QUnit/qunit.ext.js" type="text/javascript"></script>
		<script type="text/javascript">
			ExoWeb.Mock.types({
				"Core.Person": {
					properties: {
						All: { type: "Core.Person", isList: true, isStatic: true },
						Name: { type: "String", rules: [{ required: {} }, { stringLength: { max: 40}}] },
						BirthDate: { type: "Date", format: "ShortDate" },
						PhoneNumber: { type: "String", format: "Phone", rules: [{ required: {}}] }
					}
				},
				"MyApp.Person": {
					baseType: "Core.Person",
					properties: {
						SSN: { type: "String" },
						Contacts: { type: "MyApp.Person>Core.Person", isList: true }
					}
				},
				"MyApp.LegalGuardian": {
					baseType: "MyApp.Person>Core.Person",
					properties: {
						Address: { type: "String" }
					}
				},
				"MyApp.Dependant": {
					baseType: "MyApp.Person>Core.Person",
					properties: {
						Guardian: { type: "MyApp.LegalGuardian>MyApp.Person>Core.Person" }
					}
				}
			});

			ExoWeb.Mock.objects({
				"Core.Person": {
					static: {
						All: [
							{ id: "1", type: "MyApp.Dependant>MyApp.Person>Core.Person" },
							{ id: "2", type: "MyApp.LegalGuardian>MyApp.Person>Core.Person" }
						]
					}
				},
				"MyApp.Dependant": {
					"1": {
						Name: "Jenny",
						BirthDate: new Date("12/25/1978"),
						PhoneNumber: "800-867-5309",
						SSN: "123-45-6789",
						Contacts: [ { id: "2", type: "MyApp.LegalGuardian>MyApp.Person>Core.Person" } ],
						Guardian: { id: "2", type: "MyApp.LegalGuardian>MyApp.Person>Core.Person" }
					}
				},
				"MyApp.LegalGuardian": {
					"2": {
						Name: "Susan",
						BirthDate: new Date("01/01/1942"),
						PhoneNumber: "800-867-5309",
						SSN: "987-65-4321",
						Contacts: [ { id: "1", type: "MyApp.LegalGuardian>MyApp.Person>Core.Person" } ],
						Address: "123 Main St. Capitol City, ?? 00000-0000"
					}
				}
			});

			ExoWeb.Mock.roundtrip({
				// no behavior by default
			});

			ExoWeb.Mock.save({
				// no behavior by default
			});
		</script>
		<script type="text/javascript">
			var context = ExoWeb.context({ model: { dependant: { id: "1", from: "MyApp.Dependant", and: ["Core.Person.All"] }} });

			setupTest("test1", { description: "Simple Type Loading Tests", expect: 6 }, function() {
				ok(context.model.meta.type("MyApp.Person"), "Type should be retrievable via \"type\" method.");
				ok(MyApp.Person.meta, "Type should be retrievable directly from namespace objects.")
				ok(context.model.meta.type("MyApp.Person") === MyApp.Person.meta, "Two methods should yield equivelent results.");
				
				equals(MyApp.Person.getName(), "MyApp.Person", "Class should be registered and name should be the type name");
				equals(MyApp.Person, ExoWeb.Model.Model.getJsType("MyApp.Person"), "Type should be retrievable by name and \"getJsType\" method.");
				ok(MyApp.Person.meta.get, "Type should have a get method for retrieving instances.")
			});

			setupTest("test2", { description: "Property Chain Type Cast Tests", expect: 2 }, function() {
				var personType = ExoWeb.Model.Model.getJsType("MyApp.Person");
				var addressChain = ExoWeb.Model.Model.property("this<MyApp.LegalGuardian>.Address", personType);
				ok(addressChain, "Property Chain should be able to handle a cast expression involving a namespace.");
				ok(addressChain.value(context.model.dependant.get_Guardian()) === "123 Main St. Capitol City, ?? 00000-0000",
					"Property Chain should be able to retrieve a value involving a cast expression and namespace.");
			});

			setupTest("test3", { description: "Static Property of Namespaced Type", expect: 2 }, function() {
				ok(Core.Person.get_All().length === 2, "All list should have two entries.");
				ok(ExoWeb.Model.Model.property("Core.Person.All").value().length === 2, "Using the property method should yield the same result.");
			});

			context.ready(function() {
				executeTest("test1");
				executeTest("test2");
				executeTest("test3");
			});
		</script>
	</head>
	<body xmlns:sys="javascript:Sys" xmlns:dataview="javascript:Sys.UI.DataView" sys:activate="*">
		
		<!-- QUnit Display -->
		<h1 id="qunit-header">Test Results:</h1>
		<h2 id="qunit-banner"></h2>
		<h2 id="qunit-userAgent"></h2>
		<ol id="qunit-tests"></ol>
	</body>
</html>
